package org.i_plus.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.i_plus.Assistance;
import org.i_plus.model.GiftBean;
import org.i_plus.model.services.GiftService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class SendGiftServlet extends HttpServlet {

	private GiftService service;	
	@Override
	public void init() throws ServletException {
		WebApplicationContext context =
				WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
		this.service = (GiftService)context.getBean("giftService");
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		List<GiftBean> gift = service.find();
		List<String> giftpic = new ArrayList<String>();
		
		for(GiftBean temp:gift)
		giftpic.add(Assistance.photoByteToImgString("image/jpeg", temp.getPic()));
		
		int[] count = service.giftCount();
		
		req.setAttribute("gift", gift);
		req.setAttribute("giftpic", giftpic);
		req.setAttribute("count", count);
		
		req.getRequestDispatcher("/View/SendGift.jsp").forward(req, resp);
		return;
	}


}
